package org.idea.ietty.core;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import org.idea.ietty.container.ClassPathXmlApplicationContext;
import org.idea.ietty.container.IettyApplicationContext;

/**
 * @Author idea
 * @Date created in 10:00 下午 2020/5/2
 */
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    private IettyApplicationContext iettyApplicationContext;

    public HttpServerInitializer(IettyApplicationContext iettyApplicationContext) {
        this.iettyApplicationContext = iettyApplicationContext;
    }

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        // http 编解码
        pipeline.addLast(new HttpServerCodec());
        // http 消息聚合器   512*1024为接收的最大contentlength
        pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024));
        // 请求处理器
        pipeline.addLast(new HttpRequestHandler(iettyApplicationContext));
    }
}
